/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle csq-3@163.com
 * @since : 2015-07-27 11:35:35
 * @filename : gulpfile.js
 * @version : 
 * @description : 
 */

var gulp = require('gulp');
//var gutil = require("gulp-util");
//var webpack = require('gulp-webpack');
var webpack = require('webpack-stream');
var webpackConfig = require("./webpack.config.js");
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var minifyCss = require('gulp-minify-css');
var clean = require('gulp-clean');
var shell = require('gulp-shell');
var minimist = require('minimist');

var options = minimist(process.argv.slice(2));

//能运行但不能生成文件，需要回调函数里去处理
/*
gulp.task('webpack', function () {
    webpack(webpackConfig,null,function(err, stats) {
        if(err) throw new gutil.PluginError("webpack", err);
        gutil.log("[webpack]", stats.toString({
            //output options
            colors: true
        }));
    });
});
*/

//参考：https://github.com/shama/webpack-stream 
//如果path报错，兼容写法为path:require("path").resolve("./dist/")，既可以单独webpack又可以glup调用
gulp.task('webpack', function () {
    return gulp.src('')//从webpack配置里获取
    .pipe(webpack(require('./webpack.config.js')))
    //.pipe(rename({suffix: '.min'}))
    //.pipe(uglify())
    .pipe(gulp.dest('./dist'));
});

gulp.task('webpackprod', function () {
    return gulp.src('')//从webpack配置里获取
    .pipe(webpack(require('./webpack.config.js')))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
});

/*
gulp.task('copy',function(){
    return gulp.src('./src/resource')
    .pipe(gulp.dest('dist'));
});
*/

gulp.task('imagemin', function () {
    return gulp.src('src/resource/**/*.{jpeg,png,jpg}')
    .pipe(imagemin({
        progressive: true,
        svgoPlugins: [{removeViewBox: false}],
        use: [pngquant()]
    }))
    .pipe(gulp.dest('dist/resource/'));
});

gulp.task('cssmin', function() {
    return gulp.src('src/resource/**/*.css')
    .pipe(rename({suffix: '.min'}))
    .pipe(minifyCss())
    .pipe(gulp.dest('dist/resource/'));
});

gulp.task('distclean', function() {  
    return gulp.src('dist/*', {read: false})
    .pipe(clean());
});

gulp.task('version', shell.task([
    'echo version:'+options.version,
    'sed -i "s/?v=[0-9.]*/?v='+options.version+'/g" ./app/*.html'
]));

gulp.task('watch', function () {
    gulp.watch('src/**/*.js', ['webpack']);
    gulp.watch('src/resource/css/**/.css', ['cssmin']);
    gulp.watch('src/resource/img/*.{jpeg,png,jpg}', ['imagemin']);
});

//gulp --key1 value1 --key2 value2
gulp.task('default',['distclean'],function() {
    gulp.run('webpack','imagemin','cssmin');
});

//gulp dev --version 2.0.0   2.0传递进去会变为2   
gulp.task('dev',['distclean'],function() {
    if (options.version) {
        gulp.run('webpack','imagemin','cssmin','version');
    } else {
        gulp.run('webpack','imagemin','cssmin');
    }
});

gulp.task('prod',['distclean'],function() {
    if (options.version) {
        gulp.run('webpack','imagemin','cssmin','version');
    } else {
        gulp.run('webpack','imagemin','cssmin');
    }
});

/**
 *　先运行gulp dev --version 1.0.0
 *　在运行gulp watch
 */




